{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数据处理——map、apply，applymap",
   "id": "8255337e9b0428a9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T11:25:06.686132Z",
     "start_time": "2025-09-15T11:25:06.227988Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "path = 'D:/2506A/monty03/day16/file/'"
   ],
   "id": "39afa14db148a17f",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 1. map方法",
   "id": "a5e4c73a8d397c6d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T11:27:02.214356Z",
     "start_time": "2025-09-15T11:27:02.199133Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 男生显示先生，女生显示女士\n",
    "df = pd.read_excel(path + 'Vlookup.xlsx', sheet_name='花名册')\n",
    "print(df)\n",
    "\n",
    "df['称呼'] = df['性别'].map({'男':'先生','女':'女士'})\n",
    "print(df)"
   ],
   "id": "e906170710a17dc2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   学号   姓名 性别\n",
      "0   1  谭鑫宇  女\n",
      "1   2  聂茹凤  女\n",
      "2   3  韩耀祖  男\n",
      "3   4  李欣桐  女\n",
      "4   5  刘千琪  女\n",
      "   学号   姓名 性别  称呼\n",
      "0   1  谭鑫宇  女  女士\n",
      "1   2  聂茹凤  女  女士\n",
      "2   3  韩耀祖  男  先生\n",
      "3   4  李欣桐  女  女士\n",
      "4   5  刘千琪  女  女士\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 2. apply",
   "id": "d6eb8fcaa5f53389"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T11:29:32.592775Z",
     "start_time": "2025-09-15T11:29:32.580919Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + 'Vlookup.xlsx', sheet_name='花名册')\n",
    "print(df)\n",
    "df['称呼'] = df['性别'].apply(lambda x: '先生' if x=='男' else '女士')\n",
    "print(df)"
   ],
   "id": "a7c48445007b1b52",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   学号   姓名 性别\n",
      "0   1  谭鑫宇  女\n",
      "1   2  聂茹凤  女\n",
      "2   3  韩耀祖  男\n",
      "3   4  李欣桐  女\n",
      "4   5  刘千琪  女\n",
      "   学号   姓名 性别  称呼\n",
      "0   1  谭鑫宇  女  女士\n",
      "1   2  聂茹凤  女  女士\n",
      "2   3  韩耀祖  男  先生\n",
      "3   4  李欣桐  女  女士\n",
      "4   5  刘千琪  女  女士\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 3. applymap 是操作df的，不能操作series",
   "id": "2c6abfaa9432bac4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T11:37:16.515740Z",
     "start_time": "2025-09-15T11:37:16.503061Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + 'Vlookup.xlsx', sheet_name='成绩单')\n",
    "print(df)\n",
    "print('=' * 30)\n",
    "# df2 = df.applymap(lambda x: x + 10) # 该方法属于过时方法\n",
    "# print(df2)\n",
    "\n",
    "# df2 = df.map(lambda x :x+10)\n",
    "# print(df2)\n",
    "\n",
    "df2 = df.apply(lambda x:x +10)\n",
    "print(df2)"
   ],
   "id": "64bf61f3ce9337c5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   学号  语文  数学  英语   总分\n",
      "0   1  67  77  63  207\n",
      "1   2  81  58  65  204\n",
      "2   3  89  60  58  207\n",
      "3   4  67  61  52  180\n",
      "4   5  78  55  51  184\n",
      "==============================\n",
      "   学号  语文  数学  英语   总分\n",
      "0  11  77  87  73  217\n",
      "1  12  91  68  75  214\n",
      "2  13  99  70  68  217\n",
      "3  14  77  71  62  190\n",
      "4  15  88  65  61  194\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 补充： np.where",
   "id": "d6ce99b01ff07710"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T11:39:31.231729Z",
     "start_time": "2025-09-15T11:39:31.219764Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "df = pd.read_excel(path + 'Vlookup.xlsx', sheet_name='花名册')\n",
    "print(df)\n",
    "df['称呼'] = np.where(df['性别']=='男','先生','女士')\n",
    "print(df)"
   ],
   "id": "1ffafaff3d6eb865",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   学号   姓名 性别\n",
      "0   1  谭鑫宇  女\n",
      "1   2  聂茹凤  女\n",
      "2   3  韩耀祖  男\n",
      "3   4  李欣桐  女\n",
      "4   5  刘千琪  女\n",
      "   学号   姓名 性别  称呼\n",
      "0   1  谭鑫宇  女  女士\n",
      "1   2  聂茹凤  女  女士\n",
      "2   3  韩耀祖  男  先生\n",
      "3   4  李欣桐  女  女士\n",
      "4   5  刘千琪  女  女士\n"
     ]
    }
   ],
   "execution_count": 13
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
